package com.shenyue.nbtownlet.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivityHomeBinding;
import com.shenyue.nbtownlet.model.AppContactsModel;
import com.shenyue.nbtownlet.model.AppLoginModel;
import com.shenyue.nbtownlet.model.AppModel;
import com.shenyue.nbtownlet.model.AppProjectModel;
import com.shenyue.nbtownlet.model.AppTaskModel;
import com.shenyue.nbtownlet.model.DefaultModel;
import com.shenyue.nbtownlet.util.NetWorkUtils;
import com.shenyue.nbtownlet.util.StorageUtil;
import com.shenyue.nbtownlet.util.StringUtil;

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    private ActivityHomeBinding binding;
    private Context mContext;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_home);
        setActionBar(true, "小城镇环境综合整治");
        mContext = this;

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String login_config = bundle.getString("login_config");
            if (!StringUtil.isNull(login_config))
                StorageUtil.setLocalStorage(mContext, AppLoginModel.LOGIN_NATIVE_DATA, login_config);
        }

        //StorageUtil.setLocalStorage(mContext, AppLoginModel.LOGIN_NATIVE_DATA, null);

        binding.btnContacts.setOnClickListener(this);//通讯录
        binding.btnDoTasks.setOnClickListener(this);//已办任务
        binding.btnInformationDistribution.setOnClickListener(this);//信息报送
        binding.btnNotice.setOnClickListener(this);//通知公告
        binding.btnQuestionList.setOnClickListener(this);//问题单列表
        binding.btnProjectManagement.setOnClickListener(this);//项目管理
        binding.btnSnapshot.setOnClickListener(this);//随手拍
        binding.btnSpecialMeasures.setOnClickListener(this);//专项整治
        binding.btnSpotCheck.setOnClickListener(this);//现场检查
        binding.btnWaitTask.setOnClickListener(this);//待办任务
        binding.btnAddresseeDistribution.setOnClickListener(this);//收文管理
        binding.btnNewProject.setOnClickListener(this);//专题新增
        binding.btnCheckWork.setOnClickListener(this);//每月考勤

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (NetWorkUtils.isNetworkConnected(mContext)) {
                    BaseInfoTask task = new BaseInfoTask();
                    task.execute();
                }
            }
        }, 320);
    }

    @Override
    public void onClick(View v) {
        if (!NetWorkUtils.isNetworkConnected(this)) {
            showMsg(getString(R.string.no_network));
            return;
        }

        if (!AppLoginModel.isLogin(mContext)) {
            toActivity(LoginActivity.class);
            return;
        }

        switch (v.getId()) {
            case R.id.btn_wait_task://待办任务
                toActivity(WaitTaskActivity.class);
                break;
            case R.id.btn_do_tasks://已办任务
                toActivity(DoTaskActivity.class);
                break;
            case R.id.btn_contacts://通讯录
                toActivity(ContactsActivity.class);
                break;
            case R.id.btn_project_management://项目管理
                toActivity(ProjectManagementActivity.class);
                break;
            case R.id.btn_question_list: {//问题单列表
                toActivity(HoldListQActivity.class);
                break;
            }
            case R.id.btn_information_distribution://信息报送
                toActivity(InformationDistributionActivity.class);
                break;
            case R.id.btn_snapshot: {//随手拍
                Bundle bundle = new Bundle();
                bundle.putString(DefaultModel.PAGE_DATA, "1");
                toActivity(HoldListActivity.class, bundle);
                break;
            }
            case R.id.btn_notice: {//通知公告
                toActivity(NoticeActivity.class);
                break;
            }
            case R.id.btn_special_measures: {//专项整治
                toActivity(SpecialMeasuresActivity.class);
                break;
            }
            case R.id.btn_spot_check: {//现场检查
                Bundle bundle = new Bundle();
                bundle.putString(DefaultModel.PAGE_DATA, "0");
                toActivity(HoldListActivity.class, bundle);
                break;
            }
            case R.id.btn_addressee_distribution: {//收文管理
                toActivity(AddresseeDistributionActivity.class);
                break;
            }
            case R.id.btn_new_project: {//专题新增
                toActivity(NewDissertationActivity.class);
                break;
            }
            case R.id.btn_check_work: {//每月考勤
                toActivity(CheckWorkActivity.class);
                break;
            }
            default:
                break;
        }
    }

    /***
     * 基础信息
     */
    private class BaseInfoTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String[] objects) {
            if (StringUtil.isNull(AppModel.getNingBoAreaID(mContext)))
                AppContactsModel.GetNingBoAreaID(mContext, null, null);
            if (StringUtil.isNull(AppProjectModel.getProjectType(mContext)))
                AppProjectModel.GetProjectType(mContext, null, null);
            return null;
        }
    }
}
